/**
 * 菜单管理模块
 */

 const router = require('koa-router')()
 const menuSchema = require('./../models/menuSchema')
 const util = require('./../utils/util')
 const jwt = require('jsonwebtoken')

 router.prefix('/memus')

 // 菜单编辑、删除、新增功能
router.post('/operate', async (ctx) => {
    const { _id, action, ...params } = ctx.request.body;
    let res, info;
    try {
        if (action == 'add') {
            res = await Menu.create(params)
            info = '创建成功'
        } else if (action == 'edit') {
            params.updateTime = new Date();
            res = await Menu.findByIdAndUpdate(_id, params);
            info = '编辑成功'
        } else {
            res = await Menu.findByIdAndRemove(_id)
            await Menu.deleteMany({ parentId: { $all: [_id] } })
            info = '删除成功'
        }
        ctx.body = util.success('', info);
    } catch (error) {
        ctx.body = util.fail(error.stack);
    }

})
